package org.example.controller.impl;

import org.example.annotation.OperationLog;
import org.example.controller.DiscussionController;
import org.example.domain.Discussion;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.DiscussionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/discussions")
public class DiscussionControllerImpl implements DiscussionController {

    @Autowired
    private DiscussionServiceImpl discussionService;

    @Override
    @PostMapping()
    @PreAuthorize("hasAuthority('discussion:save')")
    public ResponseResult saveDiscussion(@RequestBody @Validated(Discussion.save.class) Discussion discussion) {
//        System.out.println(discussion);
        return discussionService.saveDiscussion(discussion);
    }

    @Override
    @PutMapping()
    @PreAuthorize("hasAuthority('discussion:update')")
    public ResponseResult updateDiscussion(@RequestBody @Validated(Discussion.update.class) Discussion discussion) {
        return discussionService.updateDiscussion(discussion);
    }

    @Override
    @PutMapping("/1/{userId}/{discussionId}")
    @PreAuthorize("hasAuthority('user:update')")
    public ResponseResult userLikeDiscussion(@PathVariable int userId,@PathVariable int discussionId) {
        return discussionService.userLikeDiscussion(userId,discussionId);
    }

    @Override
    @GetMapping("/0/{discussionId}")
    @PreAuthorize("hasAuthority('discussion:selectOne')")
    public ResponseResult getDiscussionById(@PathVariable int discussionId, HttpServletRequest request) {
        return discussionService.getDiscussionById(discussionId,request);
    }

    /***
     * 获取最新的讨论
     * @param pageSize 分页大小
     * @param currentPage 当前分页
     * @return
     */
    @Override
    @GetMapping("/3/{pageSize}/{currentPage}")
    public ResponseResult getDiscussionByPage(@PathVariable int pageSize,@PathVariable int currentPage) {
        return discussionService.getDiscussionByPage(pageSize,currentPage);
    }

    /**
     * 获取最热讨论推荐
     * @param pageSize 分页大小
     * @param currentPage 当前分页
     */
    @GetMapping("31/{pageSize}/{currentPage}")
    public ResponseResult getDiscussionByHot(@PathVariable int pageSize,@PathVariable int currentPage) {
        return discussionService.getDiscussionByHot(pageSize,currentPage);
    }

    /**
     * 获取最热讨论推荐
     * @param pageSize 分页大小
     * @param currentPage 当前分页
     */
    @GetMapping("32/{pageSize}/{currentPage}/{userId}")
    public ResponseResult getDiscussionByAdvice(@PathVariable int pageSize,@PathVariable int currentPage,@PathVariable int userId) {
        return discussionService.getDiscussionByAdvice(pageSize,currentPage,userId);
    }

    @Override
    @GetMapping("/6")
    @PreAuthorize("hasAuthority('discussion:selectAll')")
    public ResponseResult getDiscussionOrderByLikes(int currentPage) {
        return discussionService.getDiscussionOrderByLikes(currentPage);
    }

    @Override
    @GetMapping("/7")
    @PreAuthorize("hasAuthority('discussion:selectAll')")
    public ResponseResult getDiscussionOrderByClick(int currentPage) {
        return discussionService.getDiscussionOrderByClick(currentPage);
    }

    @Override
    @GetMapping("/5/{userId}/{currentPage}")
    @PreAuthorize("hasAuthority('discussion:selectOne')")
    public ResponseResult getDiscussionByUserId(@PathVariable int userId,@PathVariable int currentPage) {
        return discussionService.getDiscussionByUserId(userId,currentPage);
    }

    @Override
    @DeleteMapping("/{discussionId}")
    @PreAuthorize("hasAuthority('discussion:delete')")
    public ResponseResult deleteDiscussionById(@PathVariable int discussionId) {
        return discussionService.deleteDiscussionById(discussionId);
    }

    @Override
    @GetMapping("/1/{title}/{currentPage}")
    @PreAuthorize("hasAuthority('discussion:selectAll')")
    public ResponseResult getDiscussionByTitle(@PathVariable String title,@PathVariable int currentPage) {
        return discussionService.getDiscussionByTitle(title,currentPage);
    }

    @Override
    @GetMapping("/2/{type}/{currentPage}")
    @PreAuthorize("hasAuthority('discussion:selectAll')")
    public ResponseResult getDiscussionByType(@PathVariable String type,@PathVariable int currentPage) {
        return discussionService.getDiscussionByType(type,currentPage);
    }

    @Override
    @GetMapping("/4/{currentPage}")
    @PreAuthorize("hasAuthority('discussion:selectAll')")
    public ResponseResult getDiscussionByCondition(@RequestBody @Validated(Discussion.search.class) Discussion discussion, @PathVariable int currentPage) {
        return discussionService.getDiscussionByCondition(discussion,currentPage);
    }

    @Override
    @GetMapping("/41/{key}/{pageSize}/{currentPage}")
    public ResponseResult getDiscussionByKey(@PathVariable String key, @PathVariable int pageSize, @PathVariable int currentPage) {
        return discussionService.getDiscussionByKey(key,pageSize,currentPage);
    }

    @Override
    @GetMapping("/42/{discussionId}")
    public ResponseResult addDiscussionFromMysqlToESById(@PathVariable int discussionId) {
        return discussionService.addDiscussionFromMysqlToESById(discussionId);
    }
}
